package io.renren.common.utils; /**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

import com.aliyun.oss.model.PartETag;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Redis工具类
 *
 * @author Mark sunlightcs@gmail.com
 */
@Component
public class RedisUtils {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private ValueOperations<String, String> valueOperations;
    @Autowired
    private HashOperations<String, String, Object> hashOperations;
    @Autowired
    private ListOperations<String, Object> listOperations;
    @Autowired
    private SetOperations<String, Object> setOperations;
    @Autowired
    private ZSetOperations<String, Object> zSetOperations;
    /**  默认过期时长，单位：秒 */
    public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
    /**  不设置过期时长 */
    public final static long NOT_EXPIRE = -1;
    private final static Gson gson = new Gson();

    public void set(String key, Object value, long expire){
        valueOperations.set(key, toJson(value));
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.MINUTES);
        }
    }

    public void set(String key, Object value){
        set(key, value, DEFAULT_EXPIRE);
    }

    public <T> T get(String key, Class<T> clazz, long expire) {
        String value = valueOperations.get(key);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value == null ? null : fromJson(value, clazz);
    }

    public <T> T get(String key, Class<T> clazz) {
        return get(key, clazz, NOT_EXPIRE);
    }

    public String get(String key, long expire) {
        String value = valueOperations.get(key);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value;
    }

    public String get(String key) {
        return get(key, NOT_EXPIRE);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    /**
     * Object转成JSON数据
     */
    private String toJson(Object object){
        if(object instanceof Integer || object instanceof Long || object instanceof Float ||
                object instanceof Double || object instanceof Boolean || object instanceof String){
            return String.valueOf(object);
        }
        return gson.toJson(object);
    }

    /**
     * JSON数据，转成Object
     */
    private <T> T fromJson(String json, Class<T> clazz){
        return gson.fromJson(json, clazz);
    }

    public List getFromRT(String srt) {
        List<PartETag> list = (List<PartETag>) redisTemplate.opsForValue().get(srt);
        return list;
    }

    public void setStr(String key,Object obj){
        redisTemplate.opsForValue().set(key,obj);
    }

    public void setList(String key, List list){
        redisTemplate.opsForValue().set(key,list);
    }
    public void setList(String key, List list,long times){
        redisTemplate.opsForValue().set(key,list,times, TimeUnit.SECONDS);
    }
    public List getList(String key){
        return (List) redisTemplate.opsForValue().get(key);
    }

    public void hset(String hkey,String key,Object obj){
        redisTemplate.opsForHash().put(hkey,key,obj);
    }

    public Object hget(String hkey,String key){
        return redisTemplate.opsForHash().get(hkey,key);
    }

    public void hdel(String hkey,String key){
        redisTemplate.opsForHash().delete(hkey,key);
    }


    /**************************************************************/
    public void setBoundHash(String hkey,Object key,Object obj){
        redisTemplate.boundHashOps(hkey).put(key,obj);
    }

    public Object getBoundHash(String hkey,Object key){
        return redisTemplate.boundHashOps(hkey).get(key);
    }

    public List<Object> getBoundHashList(String hkey){
        return redisTemplate.boundHashOps(hkey).values();
    }

    public void setBoundListShuffle(String key,List list){
        redisTemplate.boundListOps(key).leftPushAll(list.toArray(new Integer[16000]));
    }

    public Object rightPop(String key){
        Object o = redisTemplate.boundListOps(key).rightPop();
        return o;
    }

    public void hIncream(String hkey, String key) {
        Integer count= (Integer) redisTemplate.opsForHash().get(hkey, key);
        if(count==null){
            count = 0;
        }
        redisTemplate.opsForHash().put(hkey,key,count+1);
    }

    public <T> T executeLuaScript(String luaScript, Class<T> returnType, String[] keys, String... argv) {
        return (T) redisTemplate.execute(RedisScript.of(luaScript, returnType),
                new StringRedisSerializer(),
                new GenericToStringSerializer<>(returnType),
                Arrays.asList(keys),
                (Object[])argv);
    }


}
